//------------------------------------------------------------------------------
// <copyright file="ImageDownloaderControl.xaml.cs" company="Damian Antonowicz">
//       copyright © 2010 Damian Antonowicz
// </copyright>
// <author>Damian Antonowicz</author>
// <email>poczta@damianantonowicz.pl</email>
//------------------------------------------------------------------------------

using System;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using DAnton.Utilities.Extensions;
using DotBeer.Business.Entities;

namespace DotBeer.UI.WPFClient
{
    /// <summary>
    /// Interaction logic for ImageDownloaderControl.xaml
    /// </summary>
    public partial class ImageDownloaderControl : UserControl
    {
        #region private members

        static ImageDownloaderControl _downCont;


        #endregion

        #region constructors

        public ImageDownloaderControl()
        {
            InitializeComponent();
            _downCont = this;
        }

        #endregion

        #region properties

        public static readonly DependencyProperty ImageUrlProperty = DependencyProperty.Register("ImageURL",
                                            typeof(string),
                                            typeof(ImageDownloaderControl),
                                            new PropertyMetadata("", new PropertyChangedCallback(ImageUrlChanged)));

        public string ImageURL
        {
            set { SetValue(ImageUrlProperty, value); }
            get { return (string)GetValue(ImageUrlProperty); }
        }

        #endregion

        #region public methods



        #endregion

        #region private methods

        private static void ImageUrlChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != null)
            {
                SetProgressBar();
                SetImage(e.NewValue.ToString());
            }
        }

        private static void SetImage(object ob)
        {
            string url = ob.ToString();

            ThreadPool.QueueUserWorkItem(new WaitCallback((object o) =>
            {
                _downCont.InvokeIfNeeded((Action)(() =>
                {
                    Image image = new Image();
                    BitmapImage bitmap = new BitmapImage();

                    bitmap.DownloadCompleted += delegate(object sender, EventArgs e)
                    {
                        image.Source = bitmap;
                        _downCont.LayoutRoot.Children.Clear();
                        _downCont.LayoutRoot.Children.Add(image);
                    };

                    bitmap.DownloadFailed += delegate(object sender, ExceptionEventArgs e)
                    {
                        SetQuestionMark();
                    };

                    try
                    {
                        bitmap.BeginInit();
                        bitmap.UriSource = new Uri(url);
                        bitmap.EndInit();
                    }
                    catch
                    {
                        SetQuestionMark();
                    }
                }));
            }));
        }

        private static void SetQuestionMark()
        {
            _downCont.LayoutRoot.Children.Clear();
            _downCont.LayoutRoot.Children.Add(new Image() { Source = new BitmapImage(new Uri(ApplicationPaths.QuestionMark)) });
        }

        private static void SetProgressBar()
        {
            _downCont.InvokeIfNeeded((Action)(() => 
                    { 
                        _downCont.LayoutRoot.Children.Clear();
                        _downCont.LayoutRoot.Children.Add(new CircleProgressBarControl() { Width = 40, Height = 40 });
                    }));
        }

        #endregion
    }
}
